package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.BabyInoculate;
import cn.b.training.baby_education_training.service.BabyInoculateService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 13:44
 **/
@RestController
@RequestMapping("/inoculate")
public class BabyInoculateController {
    @Autowired
    private BabyInoculateService babyInoculateService;

    @PostMapping
    public ResponseEntity addInoculate(@RequestBody BabyInoculate babyInoculate){
        try {
            boolean b = babyInoculateService.addInoculate(babyInoculate);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","添加成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","添加失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping
    public ResponseEntity getInoculateByBabyId(Long babyId) {
        try {
            List<BabyInoculate> inoculates = this.babyInoculateService.getInoculateByBabyId(babyId);
            return ResponseEntity.ok(inoculates);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }
    @GetMapping("/id")
    public ResponseEntity getInoculateByBabyIdAndVaccineId(Long babyId,Long vaccineId) {
        try {
            List<BabyInoculate> inoculates = this.babyInoculateService.getInoculateByBabyIdAndVaccineId(babyId,vaccineId);
            return ResponseEntity.ok(inoculates);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping
    public ResponseEntity DeleteInoculateById(Long id){
        try {
            boolean b = babyInoculateService.deleteInoculateById(id);
            if (b) {
                JSONObject result = new JSONObject();
                result.put("success", "删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        }catch (Exception e){
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }
}
